package com.bn.user.login;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.bn.user.dao.UserDao;
import com.bn.user.vo.Member;

/**
 * Handles requests for the application home page.
 */
@Controller
public class LoginSessionController extends SimpleFormController{
	
	@Autowired
	private SqlSession sqlSession;
	
	/*
	@Autowired
    private UserInfo userInfo = null;
	
	public void setUserInfo(UserInfo userInfo){
		this.userInfo = userInfo;
	}
	
	
	@Override
	protected ModelAndView onSubmit(HttpServletRequest request, 
									HttpServletResponse response, Object command, BindException errors) throws Exception{
		
		Member memberVO = (Member) command;
		try{
			Member loginUser = userInfo.getUserInfo(memberVO.getUserId(), memberVO.getPassword());
			
			ModelAndView modelAndView = new ModelAndView(getSuccessView(), "loginUser", loginUser);
			
			return modelAndView;
			
		}catch(DataRetrievalFailureException e){
			return showForm(request, response,errors);
		}
		
	}
	*/
/* */
    // 세션사용 화면
    @RequestMapping("page1")
    public String page1() {
        return "page1";
    }
    // 세션 사용 안하는 화면
    @RequestMapping("page2")
    public String page2() {
        return "page2";
    }
    // 로그인 화면
    @RequestMapping("login")
    public String login() {
        return "login";
    }
    // 로그아웃
    @RequestMapping("logout")
    public String logout(HttpSession session) {
        session.setAttribute("userLoginInfo", null);
        return "redirect:login";
    }
 
    // 로그인 처리
    @RequestMapping(value="loginProcess", method = RequestMethod.POST)
    public ModelAndView loginProcess(Member member, HttpSession session, HttpServletRequest request) {
        ModelAndView mav = new ModelAndView();
        //mav.setViewName("redirect:login");
        mav.setViewName("index");
        System.out.println(member.getUserId() +  member.getPassword());
        UserDao userDao = sqlSession.getMapper(UserDao.class);
        Member loginUser = userDao.findByUserIdAndPassword(member);
 
        if (loginUser != null) {
            session.setAttribute("userLoginInfo", loginUser);
        }
        return mav;
    }
    
}
